<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="crmProductSort" class="menu" node-key="sortId" :data="crmProductSortList" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false"
							 :filter-node-method="groupFilterNode" @node-click="groupClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.code }}</span>
								<span class="do">
									<el-button-group>
										<el-button icon="el-icon-edit" size="small" @click.stop="crmProductSortEdit(data)"></el-button>
										<el-button icon="el-icon-delete" size="small" @click.stop="crmProductSortDel(node, data)"></el-button>
									</el-button-group>
								</span>
							</span>
						</template>

					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" style="width: 100%;" @click="addCrmProductSort">添加分类</el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="addCrmProduct">创建</el-button>
					<sc-upload-file :apiObj="this.$API.crm.crmProduct.importCrmProductInfo" v-model="fileList" :limit="1" accept=".xml" style="margin:0px 10px 0px 10px;"
									:on-success="importCrmProductInfoRefresh"
									:show-file-list="false">
						<el-button type="success" icon="sc-icon-download">导入</el-button>
					</sc-upload-file>

					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="物资名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" row-key="productId" @selection-change="selectionChange" stripe remoteSort>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="物资编码" prop="materialCode" width="150" sortable='custom' :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="产品名称" prop="name" width="200" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="产品分类" prop="sortName" width="100" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="品牌" prop="brand" width="150" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="规格型号" prop="model" width="200" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="价格" prop="price" width="150">
						<template #default="scope">
							{{scope.row.price}}/元
						</template>
					</el-table-column>
					<el-table-column label="指导价" prop="guidePrice" width="150">
						<template #default="scope">
							{{scope.row.guidePrice}}/元
						</template>
					</el-table-column>
					<el-table-column label="备注" prop="remark" width="300" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="170">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="edit_table(scope.row, scope.$index)">编辑</el-button>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-popconfirm title="确定删除吗？此操作不可恢复！" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<crmProductSortDialog v-if="dialog.crmProductSort" ref="crmProductSortDialog" @success="handleSortSuccess" @closed="dialog.crmProductSort=false"></crmProductSortDialog>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<recordDetails v-if="dialog.detailsSave" ref="detailsDialog" @closed="dialog.detailsSave=false"></recordDetails>
</template>
<script>

import crmProductSortDialog from "./crmProductSort";
import saveDialog from "./save";
import recordDetails from './details'

export default {
	name: 'Crm Product Manage',
	components: {
		crmProductSortDialog,
		saveDialog,
		recordDetails
	},
	data() {
		return {
			apiObj: this.$API.crm.crmProduct.getCrmProductListBySortId,
			selection: [],
			dialog: {
				crmProductSort: false,
				save: false,
				detailsSave:false,
			},
			showGrouploading: false,
			groupFilterText: '',
			fileList:[],
			crmProductSortList: [],
			groupsProps: {
				label: "sortName",
				value: "sortId"
			},
			search: {
				keyword: null
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.crmProductSort.filter(val);
		}
	},
	mounted() {
		this.getCrmProductSortTree()
	},
	methods: {
		//加载树数据
		async getCrmProductSortTree() {
			this.showGrouploading = true;
			var res = await this.$API.crm.crmProductSort.getCrmProductSortTree.get();
			this.showGrouploading = false;
			this.crmProductSortList = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.sortName.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			this.$refs.table.reload({
				sortId: data.sortId
			})
		},
		//本地更新数据
		handleSuccess(data, mode) {
			this.$refs.table.refresh();
		},
		importCrmProductInfoRefresh() {
			this.$refs.table.refresh();
		},
		async handleSortSuccess() {
			var res = await this.$API.crm.crmProductSort.getCrmProductSortTree.get();
			if (res.code == 200) {
				this.crmProductSortList = res.data;
			}
		},
		addCrmProductSort() {
			this.dialog.crmProductSort = true
			this.$nextTick(() => {
				this.$refs.crmProductSortDialog.open()
			})
		},
		crmProductSortEdit(data) {
			this.dialog.crmProductSort = true
			this.$nextTick(() => {
				var editNode = this.$refs.crmProductSort.getNode(data.sortId);
				var editNodeParentId = editNode.level == 1 ? '0' : editNode.parent.data.sortId;
				data.parentId = editNodeParentId
				this.$refs.crmProductSortDialog.open("edit").setData(data)
			})

		},
		crmProductSortDel(node, data) {
			this.$confirm(`确定删除 ${data.name} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.crm.crmProduct.deleteCrmProductSort.post({sortId: data.sortId});
				if (res.code == 200) {
					this.$refs.crmProductSort.remove(data.sortId)
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}

			}).catch(() => {

			})
		},
		addCrmProduct() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		edit_table(row, index) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row)
			})
		},
		batch_del() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const loading = this.$loading();
				var idsArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					idsArr.push(this.selection[i].productId);
				}
				var res = await this.$API.crm.crmProduct.deleteCrmProductByIds.post({ids: idsArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		async table_del(row, index) {
			const loading = this.$loading();
			var res = await this.$API.crm.crmProduct.deleteCrmProduct.post({productId: row.productId});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			loading.close();
		},
		table_show(row, index) {
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open().setData(row)
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
	}
}
</script>
<style scoped>
.custom-tree-node {
	display: flex;
	flex: 1;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	padding-right: 24px;
	height: 100%;
}

.custom-tree-node .code {
	font-size: 12px;
	color: #999;
}

.custom-tree-node .do {
	margin-left: 10px;
	display: none;
}

.custom-tree-node:hover .code {
	display: none;
}

.custom-tree-node:hover .do {
	margin-left: 10px;
	display: inline-block;
}
</style>
